package com.group01.config;

import com.group01.utils.CommonUtils;
import com.group01.utils.IP.IpUtils;
import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.OperatingSystem;
import nl.bitwalker.useragentutils.UserAgent;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Author 翟玉龙
 * @Date 2024/6/21
 */
@Aspect
@Component
public class AopConfig {
    //声明控制层切点
    private final String operateLogPoint="execution(* com.group01.web.controller.*.*(..))";
    private final String serviceLogPoint="execution(* com.zyl.service.impl.*.*(..))";
    private final String categoryTreePoint="execution(* com.group01.vo.Category.*.*(..))";
    @Pointcut(operateLogPoint)//切点
    public void webLog(){

    }

    @Pointcut(serviceLogPoint)//切点
    public void serviceLog(){

    }

    @Pointcut(categoryTreePoint)//切点
    public void categoryTreePoint(){

    }

    //webLog() || categoryTreePoint()
    @Around("serviceLog()")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes instanceof ServletRequestAttributes) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();

            // 获取方法名
            String methodName = joinPoint.getSignature().getName();
            // 获取方法参数
            Object[] args = joinPoint.getArgs();
            System.out.println("开始执行方法：" + methodName);
            System.out.println("方法参数：" + java.util.Arrays.toString(args));

            // 获取请求URL
            String requestUrl = request.getRequestURI();
            System.out.println("请求URL：" + requestUrl);

            // 获取请求方法
            String requestMethod = request.getMethod();
            System.out.println("请求方法：" + requestMethod);

            // 获取IP地址
            String ip = IpUtils.getIpAddr(request);
            System.out.println("IP地址：" + ip);
            System.out.println("IpUtils.getIp(request.getRemoteAddr()) = " + IpUtils.getIp(request.getRemoteAddr()));

            // 获取详细请求地址
            String requestPath = CommonUtils.getRequestUrl(request);
            System.out.println("请求路径地址：" + requestPath);

            // 解析UserAgent
            UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));

            // 获取浏览器对象
            Browser browser = userAgent.getBrowser();
            // 获取浏览器名称
            String browserName = browser.getName();
            System.out.println("浏览器名称: " + browserName);

            // 获取操作系统对象
            OperatingSystem operatingSystem = userAgent.getOperatingSystem();
            // 获取操作系统名称
            String osName = operatingSystem.getName();
            System.out.println("操作系统名称: " + osName);

            // 记录开始时间
            long startTime = System.currentTimeMillis();

            // 执行方法
            Object result = joinPoint.proceed();

            // 记录结束时间
            long endTime = System.currentTimeMillis();

            System.out.println("结束执行方法：" + methodName);
            System.out.println("执行结果：" + result);
            System.out.println("执行耗时：" + (endTime - startTime) + "ms");

            return result;
        } else {
            // 如果没有请求对象，可能是非HTTP环境，需要特殊处理
            throw new IllegalStateException("No HttpServletRequest available in the current thread");
        }
    }
}
